home *** CD-ROM | disk | FTP | other *** search
- Path: castle.nando.net!news
- From: actuary@nando.net (Bill McCarthy)
- Newsgroups: comp.lang.c
- Subject: Re: my atoi function, could someone suggest...
- Date: 4 Jan 1996 02:57:22 GMT
- Organization: News & Observer Public Access
- Message-ID: <4cffmi$rv2@castle.nando.net>
- References: <4cf7ap$q4u@kaleka.seanet.com>
- Reply-To: actuary@nando.net (Bill McCarthy)
- NNTP-Posting-Host: grail2007.nando.net
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <4cf7ap$q4u@kaleka.seanet.com>, mitchell@seanet.com (where am i?) writes:
- >Hello. This is my first attempt at an atoi function. Although it
- >works and behaves just like atoi, I was wondering if anyone would care
- >for a look and make suggestions back to me about its efficiency.
- >
- >int atoi ( char *change )
- >{
- > int newint = 0, sign = 1;
- >
- > while ( *change )
- > {
- > if ( (*change < '0' || *change > '9') && *change != '-' )
- > {
- > *change++;
- > }
- > else
- > {
- > if ( *change == '-' )
- > {
- > *change++;
- > if ( *change >= '0' && *change <= '9' )
- > sign = -1;
- > }
- > if ( *change >= '0' && *change <= '9' )
- > {
- > while ( sign )
- > {
- > newint *= 10;
- > newint = newint + *change - 48;
- > *change++;
- > if ( *change < '0' || *change > '9' )
- > {
- > return ( newint * sign );
- > exit (0);
- > }
- > }
- > }
- > }
- > }
- > return ( 0 );
- >}
-
- I'm surprised your compiler didn't help you out. You use exit() without
- including <stdlib.h>, the function can never get to exit(), you include
- the statement *change++ three times which is useless except for its
- side effect.
-
- *change++ fetches and discards the char at change, then increments
- change. That isn't particularly efficient, but may be optimized away.
-
- More important is that your function does not duplicate the real atoi().
- Try "x89" as input. The real atoi correctly returns 0.
-
- Bill McCarthy
- actuary@nando.net
- Wendell, NC USA
-
-